package com.etlmaster.datax.plugins.tranformer;

import com.dp.nebula.wormhole.common.interfaces.ILine;
import com.dp.nebula.wormhole.common.interfaces.IParam;
import com.etlmaster.executor.utils.LogWriter;

/**
 * 行级处理 脱敏
 * @author wugf
 * @date 2016-04-29
 */
@ComponentAnnotation("desensitization")
public class TransFieldDesensitization extends LineTransProcessor {

	private int start_position;
	private int end_position;

	public TransFieldDesensitization(Integer fieldIndex, String changeCode,
			String paramStr, IParam param) {
		super(fieldIndex, changeCode, paramStr, param);
	}

	@Override
	public void transField(ILine line,IParam param) {
		String sourceValue = line.getField(getFieldIndex());
		String targetValue=changeChar(sourceValue);
		line.replaceField(targetValue, getFieldIndex());

	}

	private String changeChar(String sourceValue) {
		char[] source = sourceValue.toCharArray();
		if ( start_position < source.length && end_position < source.length) {
			for (int i = start_position; i < end_position; i++) {
				source[i] = '*';
			}
		}else{
			LogWriter.addLog("ERROR","输入参数不合法，不进行脱敏");
		}
		return String.valueOf(source);
	}

	public int getStart_position() {
		return start_position;
	}

	public void setStart_position(int start_position) {
		this.start_position = start_position;
	}

	public int getEnd_position() {
		return end_position;
	}

	public void setEnd_position(int end_position) {
		this.end_position = end_position;
	}

	@Override
	public boolean isSkip() {
		if(start_position > 0 && end_position > 0 && end_position > start_position)
			return false;
		else
			return true;
	}

}
